package cn.edu.sgu.www.mhxysy.service.partner.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.partner.PartnerSkillCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.partner.PartnerSkillCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.partner.PartnerSkillCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.partner.PartnerSkillCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.partner.PartnerSkillCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PartnerSkillCategoryServiceImpl implements PartnerSkillCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final PartnerSkillCategoryMapper partnerSkillCategoryMapper;

    @Autowired
    public PartnerSkillCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            PartnerSkillCategoryMapper partnerSkillCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.partnerSkillCategoryMapper = partnerSkillCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_PARTNER_SKILL;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(PartnerSkillCategoryDTO partnerSkillCategoryDTO) {
        QueryWrapper<PartnerSkillCategory> queryWrapper = new QueryWrapper<>();
        String name = partnerSkillCategoryDTO.getName();

        queryWrapper.eq("name", name);

        // 判断技能名称是否重复
        Long count = partnerSkillCategoryMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.CONFLICT, "技能" + name + "已存在。");
        }

        PartnerSkillCategory partnerSkillCategory = toEntity(partnerSkillCategoryDTO);

        partnerSkillCategoryDTO.setId(null);

        partnerSkillCategoryMapper.insert(partnerSkillCategory);
    }

    @Override
    public void deleteById(Integer id) {
        partnerSkillCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(PartnerSkillCategoryDTO partnerSkillCategoryDTO) {
        ObjectUtils.requireNonNull(partnerSkillCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        PartnerSkillCategory partnerSkillCategory = toEntity(partnerSkillCategoryDTO);

        partnerSkillCategoryMapper.updateById(partnerSkillCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<PartnerSkillCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                partnerSkillCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<PartnerSkillCategory> selectList(String name) {
        QueryWrapper<PartnerSkillCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(
                StringUtils.isNotEmpty(name),
                "name", name
        );

        return partnerSkillCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<PartnerSkillCategory> selectByPage(PartnerSkillCategoryPager pager) {
        QueryWrapper<PartnerSkillCategory> queryWrapper = new QueryWrapper<>();
        Page<PartnerSkillCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getName()),
                "name", pager.getName()
        );

        return partnerSkillCategoryMapper.selectPage(page, queryWrapper);
    }

    private PartnerSkillCategory toEntity(PartnerSkillCategoryDTO partnerSkillCategoryDTO) {
        PartnerSkillCategory partnerSkillCategory = new PartnerSkillCategory();

        partnerSkillCategory.setId(partnerSkillCategoryDTO.getId());
        partnerSkillCategory.setName(partnerSkillCategoryDTO.getName());
        partnerSkillCategory.setNote(partnerSkillCategoryDTO.getNote());

        return partnerSkillCategory;
    }

}